package autoteam.ui.tabs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;

import ui.TablePanel;
// import autoteam.Coach;
import autoteam.League;
import autoteam.Nights;
import autoteam.tables.AutoTeamTableModel;
import autoteam.tables.CoachTableModel;
import autoteam.ui.AutoTeamTablePanel;
import autoteam.ui.dialogs.CoachCreator;
import autoteam.ui.dialogs.CoachEditor;
import autoteam.ui.dialogs.CoachProfileEditor;
import autoteam.ui.dialogs.profiles.CoachProfile;
import autoteam.ui.dialogs.profiles.ProfileDialog;
import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class CoachesTab extends TabPanel
{
    private static final long serialVersionUID = -4617398074187786398L;
    
    // private String correctPath;
    
    private JButton addButton;
    private JButton removeButton;
    private JButton editButton;
    private JButton profileButton;
    private JButton editProfileButton;
    
    private JTable table; // << table will eventually hold a reference to the
    // JTable that is created in AutoTeamTablePanel.
    // This reference is needed for dialogs.
    private AutoTeamTableModel coachModel;
    // private Coach c_info = new Coach( );
    
    public CoachesTab( JFrame owner )
    {
        super( );
        
        addButton = new JButton( "Add" );
        removeButton = new JButton( "Remove" );
        editButton = new JButton( "Edit" );
        profileButton = new JButton( "Profile" );
        editProfileButton = new JButton( "Edit Profile" );
        
        TablePanel theTablePanel = new TablePanel( coachModel, editButton, addButton, removeButton, editProfileButton );
        
        this.add( theTablePanel );
    }
    
    public CoachesTab( League currentLeague, JFrame owner )
    {
        JComboBox nightComboBox = new JComboBox( Nights.night );
        
        coachModel = new CoachTableModel( currentLeague );
        /*
         * System.out.println("EDIT_BUTTON: " + editButton);
         * System.out.println("ADD_BUTTON: " + addButton);
         * System.out.println("REMOVE_BUTTON: " + removeButton);
         * System.out.println("PROFILE_BUTTON: " + profileButton);
         */
        editButton = new JButton( "Edit" );
        addButton = new JButton( "Add" );
        removeButton = new JButton( "Remove" );
        profileButton = new JButton( "Profile" );
        editProfileButton = new JButton( "Edit Profile" );
        final AutoTeamTablePanel theTablePanel = 
            new AutoTeamTablePanel( coachModel, editButton, addButton, removeButton, profileButton, editProfileButton );
        this.add( theTablePanel );
        table = theTablePanel.getTable( );
        setButtonActions( owner );
        /*
         * JButton coach_button = new JButton("Coach Info");
         * coach_button.addActionListener(new ActionListener() { public void
         * actionPerformed(ActionEvent e) { int r =
         * theTablePanel.getSelectedRow();
         * 
         * if (r < 0) { JOptionPane.showMessageDialog(null,
         * Local.get("NoSelectionEd"), Local.get("Error"),
         * JOptionPane.ERROR_MESSAGE, null); return; }
         * pop_up_info(coachModel.getValueAt(r, 3).toString()); } });
         * 
         * theTablePanel.addButton(coach_button); JComboBox playerComboBox = new
         * JComboBox(currentLeague.getChildren());
         * theTablePanel.setTableCellEditor(2, playerComboBox);
         * 
         * JButton edit_coach = new JButton("Edit Coach");
         * edit_coach.addActionListener(new ActionListener() { public void
         * actionPerformed(ActionEvent e) { int r =
         * theTablePanel.getSelectedRow();
         * 
         * if (r < 0) { JOptionPane.showMessageDialog(null,
         * Local.get("NoSelectionEd"), Local.get("Error"),
         * JOptionPane.ERROR_MESSAGE, null); return; }
         * edit_pop_up(coachModel.getValueAt(r, 3).toString()); } });
         * 
         * theTablePanel.addButton(edit_coach);
         */

    }
    
//    public void pop_up_info( String name )
//    {
//        CoachProfile cp = new CoachProfile( name );
//        cp.changeProfilePic( c_info.getPicturePath( name ), 120, 120 );
//        cp.setBiography( c_info.getBiography( name ), new Font( "sansserif", Font.PLAIN, 11 ), false );
//    }
    
    @ Override
    protected void setButtonActions( final JFrame parentFrame )
    {
        /* ADD */
        addButton.addActionListener( new ActionListener( )
        {
            @ Override
            public void actionPerformed( ActionEvent arg0 )
            {
                if ( table.isEditing( ) )
                {
                    table.getCellEditor( ).stopCellEditing( );
                }
                int j = table.getSelectedRow( );
                coachModel.fireTableDataChanged( );
                // pull up a CoachCreator doohigy
                CoachCreator dialog = new CoachCreator( parentFrame, coachModel );
                dialog.setVisible( true );
            }
        } );
        
        /* EDIT */
        editButton.addActionListener( new ActionListener( )
        {
            @ Override
            public void actionPerformed( ActionEvent e )
            {
                // pull up a CoachEditor doohigy
                int row = table.getSelectedRow( );
                CoachEditor editDialog = new CoachEditor( parentFrame, coachModel, row );
                editDialog.setVisible( true );
            }
        } );
        
        /* REMOVE */
        removeButton.addActionListener( new ActionListener( )
        {
            @ Override
            public void actionPerformed( ActionEvent e )
            {
                // just remove it.
                int r = table.getSelectedRow( );
                coachModel.delete( r );
            }
        } );
        
        /* PROFILE */
        profileButton.addActionListener( new ActionListener( )
        {
            
            @ Override
            public void actionPerformed( ActionEvent e )
            {
                int row = table.getSelectedRow( );
                // String name = coachModel.getValueAt( row, 3 ).toString( );
                
                DataAccessObject dao = new DataAccessObject( );
                DBRecord coachRecord = dao.getRecord( "coaches", new ColumnData( "id", coachModel.getValueAt( row, 0 ) ) );
                
                CoachProfile profile = new CoachProfile( coachRecord );
                
                ProfileDialog dialog = new ProfileDialog( profile );
                dialog.setVisible( true );
            }
            
        } );
        
        editProfileButton.addActionListener( new ActionListener( )
        {
            
            @ Override
            public void actionPerformed( ActionEvent e )
            {
                int row = table.getSelectedRow( );
                CoachProfileEditor editProfileDialog = new CoachProfileEditor( (Integer) coachModel.getValueAt( row, 0 ) );
            }
            
        } );
    }
}
